//
//  AWVoiceChatMessage.m
//  AtWork2
//
//  Created by lucas on 15/5/28.
//  Copyright (c) 2015年 Foreveross. All rights reserved.
//

#import "AWVoiceChatMessage.h"

@implementation AWVoiceChatMessage

+ (instancetype)voiceChatMessageWithFrom:(NSString *)from
                                      to:(NSString *)to
                              deliveryId:(NSString *)deliveryId
                                duration:(NSUInteger)duration
                            chatDescType:(AWChatDestType)chatDestType {
    return [[self alloc] initWithFrom:from to:to deliveryId:deliveryId duration:duration chatDescType:chatDestType];
}

+ (instancetype)voiceServeAppWithFrom:(NSString *)from
                                   to:(NSString *)to
                           deliveryId:(NSString *)deliveryId
                             duration:(NSUInteger)duration {
    return [[self alloc] initServeAppWithFrom:from to:to deliveryId:deliveryId duration:duration];
}

- (instancetype)initServeAppWithFrom:(NSString *)from
                                  to:(NSString *)to
                          deliveryId:(NSString *)deliveryId
                            duration:(NSUInteger)duration {
    if (self = [super initWithDeliveryId:deliveryId from:from to:to deliveryTime:deliveryTime() messageType:AWMessageTypeServeNo]) {
        self.chatMessageType = AWChatMessageTypeVoice;
        self.duration = duration;
    }
    return self;
}

- (instancetype)initWithFrom:(NSString *)from
                                      to:(NSString *)to
                              deliveryId:(NSString *)deliveryId
                                duration:(NSUInteger)duration
                            chatDescType:(AWChatDestType)chatDestType {
    if (self = [super initWithDeliveryId:deliveryId from:from to:to deliveryTime:deliveryTime() messageType:AWMessageTypeChat]) {
        self.chatDestType = chatDestType;
        self.chatMessageType = AWChatMessageTypeVoice;
        self.duration = duration;
    }
    return self;
}

#pragma mark - Getter Setter Methods

- (void)setMediaId:(NSString *)mediaId {
    [self bodyWithKey:AWMessageKMediaId value:mediaId];
}

- (NSString *)mediaId {
    return [self bodyValueForKey:AWMessageKMediaId];
}

- (void)setDuration:(NSUInteger)duration {
    [self bodyWithKey:AWMessageKDuration value:@(duration)];
}

- (NSUInteger)duration {
    return [[self bodyValueForKey:AWMessageKDuration] unsignedIntegerValue];
}

- (NSData *)content {
    if(self.body[AWMessageKContent]){
        return base64WithString(self.body[AWMessageKContent]);
    }
    return [NSData data];
}

- (void)setPlayed:(BOOL)played{
    if (played) {
        [self bodyWithKey:@"played" value:@"YES"];
    }else{
        [self bodyWithKey:@"played" value:@"NO"];
    }
    
}

-(BOOL)isPlayed {
    NSString *played = [self.body objectForKey:@"played"];
    if (played==nil || [played isEqualToString:@"NO"]) {
        return NO;
    }
    return YES;
}

@end
